import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Login
 * 
 * This is a simple example on how to use Servlets and JSP. 
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * Default Servlet constructor
     * 
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
    }

	/**
	 * This method processes a GET request from a web page
	 * It also counts page accesses in a session.
	 * 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		PrintWriter out = response.getWriter();
	    response.setContentType("text/html");
	    
	    session.setAttribute("accessCount", countAccess(session)); 
	    
	    out.println("<H1>GET-Hello from my servlet</H1>");
	    out.println("<br />Name = " + request.getParameter("name"));
	}

	/**
	 * This method processes a POST request from a web page.
	 * It checks login parameters and counts page accesses in a session. 
	 * 
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// get session
		HttpSession session = request.getSession(true);
		
		// if this is a new session set login to false
		if(session.getAttribute("loginCorrect") == null)
			session.setAttribute("loginCorrect", false); // set login variable

		// get login parameters
		String username = request.getParameter("username");  
		String password = request.getParameter("password");  
		
		// set page access count
		session.setAttribute("accessCount", countAccess(session));
		
		// check login parameters
		if(username != null && password != null ){
			if(username.equals("manager") && password.equals("manager")){
				//overwrite login variable
			    session.setAttribute("loginCorrect", true);
			}
		}
		
		// return to index page
		response.sendRedirect("index.jsp");
	}
	
	/**
	 * This method calculates the number of page accesses in the current session.
	 * 
	 * @param session - the current session
	 * @return Integer - number of page accesses
	 */
	private Integer countAccess(HttpSession session){
		Integer accessCount = new Integer(0);
	    
	    Integer oldAccessCount =
	      (Integer)session.getAttribute("accessCount"); 
	    if (oldAccessCount != null) {
	      accessCount =
	        new Integer(oldAccessCount.intValue() + 1);
	    }
	    
	    return accessCount;
	}
}
